package com.neusoft.neuiotms.common.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.common.dto.DictionaryDTO;
import com.neusoft.neuiotms.common.model.Dictionary;
import com.neusoft.neuiotms.common.repository.DictionaryRepository;

@Component
public class DictionaryConverter extends AbstractConverter<Dictionary, DictionaryDTO> {

    @Autowired
    private DictionaryRepository dictionaryRepository;

    @Override
    public Dictionary toModel(final DictionaryDTO dto) {
        Dictionary model;
        if (dto.isNew()) {
            model = new Dictionary();
        } else {
            model = this.dictionaryRepository.getOne(dto.getId());
        }

        model.setCode(dto.getCode());
        model.setSort(dto.getSort());
        model.setText(dto.getText());
        return model;
    }

    @Override
    public DictionaryDTO toDTO(final Dictionary model, final boolean forListView) {

        final DictionaryDTO dto = new DictionaryDTO();

        dto.setId(model.getId());
        dto.setCode(model.getCode());
        dto.setSort(model.getSort());
        dto.setText(model.getText());
        dto.setType(model.getDictionaryType().getType());
        dto.setTypeDesc(model.getDictionaryType().getText());
        return dto;
    }

}
